
--复眼勋章/夜视-------------------------------------------------------------------------------------------------------------------
--注释部分为能力勋章代码，但是后来发现更简单的写法
--复眼滤镜
local OMMATEUM_COLOURCUBES = {
    -- day = "images/colour_cubes/purple_moon_cc.tex",
    -- dusk = "images/colour_cubes/purple_moon_cc.tex",
    -- night = "images/colour_cubes/purple_moon_cc.tex",
    -- full_moon = "images/colour_cubes/purple_moon_cc.tex",

    day = "images/colour_cubes/spring_day_cc.tex",
    dusk = "images/colour_cubes/spring_dusk_cc.tex",
    night = "images/colour_cubes/purple_moon_cc.tex",
    full_moon = "images/colour_cubes/purple_moon_cc.tex",
}
local ORIGIN_COLOURCUBES = {
    day = "images/colour_cubes/identity_colourcube.tex",
    dusk = "images/colour_cubes/identity_colourcube.tex",
    night = "images/colour_cubes/identity_colourcube.tex",
    full_moon = "images/colour_cubes/identity_colourcube.tex",
}
--复眼函数
local function SetOmmateumVision(inst)
    if inst.components.playervision then
        if inst.starry_hat_buff and inst.starry_hat_buff:value() then
            inst.components.playervision:ForceGoggleVision(true)
            inst.components.playervision:PushForcedNightVision("starry_hat_buff", 10,
                ORIGIN_COLOURCUBES or OMMATEUM_COLOURCUBES, true)
        else
            inst.components.playervision:ForceGoggleVision(false)
            inst.components.playervision:PopForcedNightVision("starry_hat_buff")
        end
    end
end
AddPlayerPostInit(function(inst)
    if inst and inst.prefab == "starry" and inst.prefab == "starry" then
        inst.starry_hat_buff = GLOBAL.net_bool(inst.GUID, "starry_hat_buff", "starry_hat_buff")
        inst:ListenForEvent("starry_hat_buff", SetOmmateumVision) --复眼勋章
        --戴了复眼勋章就算作处于光亮中了
        local oldIsInLight = inst.IsInLight
        if oldIsInLight ~= nil then
            inst.IsInLight = function(inst, ...)
                if inst.starry_hat_buff and inst.starry_hat_buff:value() then
                    return true
                end
                return oldIsInLight(inst, ...)
            end
        end
    end
end)
------------------------------------------------------------------------------------------------------
-------------------------------------------滤镜防神话刷帧----------------------------------------------
------------------------------------------------------------------------------------------------------
AddComponentPostInit("playervision", function(self)
    local oldForceGoggleVision = self.ForceGoggleVision
    if oldForceGoggleVision then
        self.ForceGoggleVision = function(self, force, ...)
            local isequipped = self.inst and self.inst.starry_hat_buff and self.inst.starry_hat_buff:value()
            force = force or isequipped
            if oldForceGoggleVision then
                oldForceGoggleVision(self, force, ...)
            end
        end
    end
    local oldForceNightVision = self.ForceNightVision
    if oldForceNightVision then
        self.ForceNightVision = function(self, force, ...)
            local isequipped = self.inst and self.inst.starry_hat_buff and self.inst.starry_hat_buff:value()
            force = force or isequipped
            if oldForceNightVision then
                oldForceNightVision(self, force, ...)
            end
        end
    end
end)
--下面是我自己的写法，滤镜会比较暗，遂放弃
-- local starry_hat_buff = function(inst) --夜视函数
--     if inst.components.playervision then
--         if inst.starry_hat_buff and inst.starry_hat_buff:value() then
--             inst.components.playervision:ForceNightVision(true)
--             inst.components.playervision:SetCustomCCTable({ night = nil })
--         else
--             inst.components.playervision:ForceNightVision(false)
--         end
--     end
-- end
-- AddPlayerPostInit(function(inst) --增加网络变量
--     if inst.prefab == "starry" then
--         inst.starry_hat_buff = GLOBAL.net_bool(inst.GUID, "starry_hat_buff", "starry_hat_buff")
--         inst:ListenForEvent("starry_hat_buff", starry_hat_buff)
--     end
-- end)
--------------------------------------------------分割线，下面放预制物

-- 开启夜视
local function EnableNightVision(owner)
    if owner and owner.starry_hat_buff ~= nil then
        owner.starry_hat_buff:set(true)
    end
end

-- 关闭夜视
local function DisableNightVision(owner)
    if owner and owner.starry_hat_buff ~= nil then
        owner.starry_hat_buff:set(false)
    end
end

-- 监听函数：夜晚开启
local function OnNight(owner)
    if owner and owner:IsValid() then
        EnableNightVision(owner)
    end
end

-- 监听函数：白天关闭
local function OnDay(owner)
    if owner and owner:IsValid() then
        DisableNightVision(owner)
    end
end
-- 绑定监听
local function BindNightVision(inst, owner)
    if TheWorld:HasTag("cave") then
        -- 洞穴默认开夜视
        EnableNightVision(owner)
    else
        -- 地面：监听昼夜
        inst._night_cb = function() OnNight(owner) end
        inst._day_cb = function() OnDay(owner) end
        owner:WatchWorldState("isnight", inst._night_cb)
        owner:WatchWorldState("isday", inst._day_cb)

        -- 初始状态
        if TheWorld.state.isnight then
            EnableNightVision(owner)
        else
            DisableNightVision(owner)
        end
    end
end

-- 解绑监听
local function UnbindNightVision(inst, owner)
    if inst._night_cb then
        owner:StopWatchingWorldState("isnight", inst._night_cb)
        inst._night_cb = nil
    end
    if inst._day_cb then
        owner:StopWatchingWorldState("isday", inst._day_cb)
        inst._day_cb = nil
    end
    DisableNightVision(owner)
end



local function onequip(inst, owner) -- 穿上时
    BindNightVision(inst, owner)
end

local function onunequip(inst, owner) -- 卸下时

    UnbindNightVision(inst, owner)
end